package com.example.controller;

import com.example.Dto.DrugDto;
import com.example.aspect.vo.DrugVo;
import com.example.entity.Drug;
import com.example.result.Result;
import com.example.service.DrugService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/drug")
@Slf4j

public class drugController {

    @Autowired
    private DrugService drugService;

    /**
     * 浏览药品相关接口
     * @param drugDto
     * @return
     */
    @PostMapping("/lookdrug_h2_16")
    public Result<List<Drug>> scan_h2_16(@RequestBody DrugDto drugDto){
        if(drugDto.drugidH216==""){
            drugDto.drugidH216=null;
        }
        if(drugDto.empidH216==""){
            drugDto.empidH216=null;
        }
        log.info("drugDto{}",drugDto);
        List<Drug> drug = drugService.scanDrug_h2_16(drugDto);

        return Result.success(drug);
    }

    /**
     * 添加修改药品相关接口
     * @param drugVo
     * @return
     */
    @PostMapping("/insertdrug_h2_16")
    public Result drugManager_h2_16(@RequestBody DrugVo drugVo){
        DrugDto drugDto = DrugDto.builder()
                .drugidH216(drugVo.getDrugidH216())
                .build();
        List<Drug> drug = drugService.scanDrug_h2_16(drugDto);
        if(drug!=null){
            drugService.deleteByDrygid_h2_16(drugVo.getDrugidH216());
        }
        drugService.insertDrug_h2_16(drugVo);
        return Result.success();
    }

    /**
     * 删除药品相关接口
     * @param drugVo
     * @return
     */
    @PostMapping("/deletedrug_h2_16")
    public Result deleteDrug_h2_16(@RequestBody DrugVo drugVo){
        drugService.deleteByDrygid_h2_16(drugVo.getDrugidH216());
        return Result.success();
    }

}
